﻿using Lamex.Library.CommonClass;
using Lamex.mIE.Calculate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Lamex.mIE.Common
{
    public class GetFeat_Value
    {
        /// <summary>
        /// 获取特征值
        /// </summary>
        /// <param name="dt">特征定义表</param>
        /// <param name="dtValueOption">特征选项表</param>
        /// <param name="Feature_Name">特征名称</param>
        /// <returns></returns>
        public static Tuple<string, string, double> Feat_Value_From(DataTable dt, DataTable dtValueOption, DataTable dtFeature_Opt_Lar, string Feature_Name, string DWG_NO, string Part_NO,string StrPROCESS_CODE)
        {
            string strType = "";
            string strValues = "";//特征值
            double strLabor = 0;//工时
            //得到值来源
            if (dt.Rows.Count > 0)
            {
                if (dt.Select("FEATURE_NAME='" + Feature_Name + "'").Length > 0)
                {
                    string values = dt.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["FEAT_VALUE_FROM"].ToString();
                    switch (values)
                    {
                        case "固定":
                            strType = "text";
                            strValues = dt.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["VALUE_LOGIC"].ToString();
                            strLabor = 1;
                            break;
                        case "输入":
                            strType = "input";
                            strValues = "";
                            strLabor = 0;
                            break;
                        case "选择":
                            strType = "combobox";
                            if (dtValueOption.Rows.Count > 0)
                            {
                                if (dtValueOption.Select("FEATURE_NAME='" + Feature_Name + "'").Length > 0)
                                    strValues = dtValueOption.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["FEAT_VALUE_OPTION"].ToString();
                            }
                            strLabor = 0;
                            break;
                        case "选类":
                            strType = "select";
                            if (dtFeature_Opt_Lar.Rows.Count > 0)
                            {
                                DataTable dtTempLar = dtFeature_Opt_Lar.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable();
                                for (int i = 0; i < dtTempLar.Rows.Count; i++)
                                {
                                    strValues = strValues + "/" + dtTempLar.Rows[i]["FEAT_VALUE_OPTION"].ToString();
                                }
                            }
                            strLabor = 0;
                            break;
                        case "逻辑":
                            //单元格【值】格式
                            strType = "text";
                            //【值】结果
                            string logic = "";
                            if (dt.Select("FEATURE_NAME='" + Feature_Name + "'") == null)
                                logic = "";
                            else
                                logic = dt.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["VALUE_LOGIC"].ToString();
                            if (logic.Contains("?"))
                            {
                                if (StrPROCESS_CODE == "D42")
                                {
                                    if (Calculate.LGC_TIM_D42_SAW.LGC_TIM_D42_SAW_VALUES(Part_NO))
                                        //strValues = IELogicHelper.GetLogicValue(DWG_NO, Part_NO, logic.Trim());
                                        strValues = "0";
                                    else
                                        // strValues = "0";
                                        strValues = IELogicHelper.GetLogicValue(DWG_NO, Part_NO, logic.Trim());
                                }
                                else
                                    strValues = IELogicHelper.GetLogicValue(DWG_NO, Part_NO, logic.Trim());
                            }
                            else
                                strValues = logic;
                            //【工时】结果
                            strLabor = GetLabor_Value(dt, strValues, Feature_Name, DWG_NO, Part_NO);
                            break;
                    }
                }
            }
            Tuple<string, string, double> tuple = new Tuple<string, string, double>(strType, strValues, strLabor);
            return tuple;
        }

        public static double GetLabor_Value(DataTable dt,string strFeature_Value, string Feature_Name, string DWG_NO, string Part_NO)
        {
            double strValues = 0;
            double  strLabor = 0;
            
             if (dt.Select("FEATURE_NAME='" + Feature_Name + "'") == null)
                 strValues = 0;
            // else
                // logic = dt.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["VALUE_LOGIC"].ToString();
             if (strFeature_Value.Contains("?"))
             {
                 if (IsNumber(IELogicHelper.GetLogicValue(DWG_NO, Part_NO, strFeature_Value.Trim())))
                     strValues = Convert.ToDouble(IELogicHelper.GetLogicValue(DWG_NO, Part_NO, strFeature_Value.Trim()));
             }
             else if (strFeature_Value == "LGC_VAL_T01_BUMPING")
             {
                 strValues = Calculate.LGC_VAL_T01_BUMPING.LGC_VAL_T01_BUMPING_VALUE(DWG_NO, Part_NO, Feature_Name);
             }
             else if (strFeature_Value == "LGC_VAL_T03_WELDING")
             {
                 strValues = Calculate.LGC_VAL_T03_WELDING.LGC_VAL_T03_WELDING_VALUE(DWG_NO, Part_NO, Feature_Name);
             }
             else
             {
                 if (IsNumber(strFeature_Value))
                     strValues = Convert.ToDouble(strFeature_Value);

             }
             string WorkLogic = dt.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["WORKTIME_LOGIC_CODE"].ToString();
             if (WorkLogic == "LGC_TIM_LABOR")
                 strLabor = LGC_TIM_LABOR.LGC_TIM_LABOR_VALUE(Feature_Name, strFeature_Value);
             else if (WorkLogic == "LGC_TIM_T02_BENDING")
                 strLabor = LGC_TIM_T02_BENDING.LGC_TIM_T02_BENDING_VALUE(DWG_NO, Part_NO, strValues.ToString());
             else if (WorkLogic == "LGC_TIM_VTA")
                 strLabor = LGC_TIM_VTA.LGC_TIM_VTA_VALUE(DWG_NO,Part_NO,Feature_Name,"", strValues);
             else
                 strLabor = 0;
             return strLabor;
        }
       //正则表达式验证是否为数字
        public static bool IsNumber(String strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
                   !objTwoDotPattern.IsMatch(strNumber) &&
                   !objTwoMinusPattern.IsMatch(strNumber) &&
                   objNumberPattern.IsMatch(strNumber);
        }
    }
}
